// components/tabs/tabs.js
import {throttle} from '../../utils/utils'
Component({

    options: {
        multipleSlots: true
    },  

    /**
     * 组件的属性列表
     */
    properties: {
        tabs: {
            type: Array,
            value: []
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        currentTabIndex: 0,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleTabChange: throttle(function (e){
            let index = e.currentTarget.dataset.index
            this.setData({
                currentTabIndex: index
            })

            this.triggerEvent("change", {index})
        }),
        handleTouchMove(e){
            const direction = e.direction
            const currentTabIndex = this.data.currentTabIndex
            const targetTabindex = currentTabIndex + direction
            if(targetTabindex < 0 || targetTabindex > this.data.tabs.length - 1){
                return
            }
            const customEvent = {
                currentTarget: {
                    dataset: {
                        index: targetTabindex
                    }
                }
            }
            this.handleTabChange(customEvent)
        }
    }
})
